//
//  DetailPicturesViewController.m
//  BeanTrip
//
//  Created by dlios on 15-7-6.
//  Copyright (c) 2015年 PandaGroup. All rights reserved.
//

#import "DetailPicturesViewController.h"
#import "DetailPicturesCollectionViewCell.h"
#import "DetailModel.h"
#import "DetailPicturesModel.h"
#import "HTTPTool.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "PictureViewController.h"
#import "MBProgressHUD.h"

#define WIDTH ([UIScreen mainScreen].bounds.size.width/375)
#define HEIGH ([UIScreen mainScreen].bounds.size.height/667)
@interface DetailPicturesViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
/**
 *  设置一个提示框属性
 */
@property(nonatomic, retain)UIAlertView *alter;
@property(nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain) MBProgressHUD *HUD;
@end

@implementation DetailPicturesViewController
- (void)dealloc
{
    [_detailPictureImageArr release];
    [_alter release];
    [_collectionView release];
    [_getTypeNumber release];
    [_getIdString release];
    [_HUD release];
    [_titName release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.detailPictureImageArr=[[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor colorWithRed:68/255.0 green:176/255.0 blue:193/255.0 alpha:1.0];
    self.navigationController.navigationBar.translucent = NO;
    self.title = self.titName;
    /**
     1.创建瀑布流效果
     */
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    /**
     2.每一个item的大小
     */
    flowLayout.itemSize=CGSizeMake((375*WIDTH-32*WIDTH)/3, (667*HEIGH-64*HEIGH)/6);
    /**
     *3.设置最小的行间距
     */
    flowLayout.minimumLineSpacing = 10;
    /**
     *4.滑动的方向(垂直滑动),默认是垂直滑动
     */
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    /**
     *5.边缘
     */
    flowLayout.sectionInset = UIEdgeInsetsMake(8*HEIGH, 4*WIDTH, 8*HEIGH, 4*WIDTH);
    /**
     *6.创建瀑布流视图
     */
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, 375*WIDTH, 667*HEIGH-64-49) collectionViewLayout:flowLayout];
    /**
     *8.签订两个协议,一个dataSource,一个delegate
     */
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    /**
     *9.把瀑布流视图添加到view上
     */
    [self.view addSubview:self.collectionView];
    [self.collectionView release];
    /**
     *设置瀑布流的背景颜色
     */
    self.collectionView.backgroundColor=[UIColor whiteColor];
    /**
     10.注册
     *第一个参数:需要指定注册的类型;第二个参数:重用标志
     */
    [self.collectionView registerClass:[DetailPicturesCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.collectionView.tag=1006;
 
    
    /**
     菊花
     */
    self.HUD = [[MBProgressHUD alloc] initWithView:self.collectionView];
    [self.collectionView addSubview:self.HUD];
    self.HUD.labelText = @"加载中";
    [self.HUD show:YES];

    //请求值
    self.next_Page = 0;
    //网络请求
    [self createData:0];

    // 给定属性一个初值
    self.isUpLoading = NO;
    // 调用俩个刷新方法
   // [self addHeader];
    [self addFooter];
   
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block DetailPicturesViewController *detailVC = self;
    // 添加上拉刷新尾部控件
    [self.collectionView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        detailVC.isUpLoading = YES;//标记为上拉操作
        [detailVC createData:detailVC.next_Page];//请求更多数据
        
    }];
}


-(void)createData:(NSInteger)pageNum
{
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com//destination/place/%@/%@/photos/?gallery_mode=1&count=18&start=%ld",self.getTypeNumber, self.getIdString, pageNum];
    self.next_Page=self.next_Page+18;
    [HTTPTool get:url body:nil httpResult:JSON success:^(id result) {
        
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.detailPictureImageArr removeAllObjects];
        }
     
        NSDictionary *dic = result;
        NSArray *dataArr=dic[@"items"];
        for (NSDictionary *temp in dataArr) {
            DetailPicturesModel *cover = [[ DetailPicturesModel alloc] initWithDic:temp];
            [self.detailPictureImageArr addObject:cover];
         
        }
        
        /**
         *  根据tag值取出collection
         *
         *  @param UICollectionView
         *
         *  @return
         */
        UICollectionView *collectionView = (UICollectionView *)[self.view viewWithTag:1006];
        /**
         *  数据刷新
         */
       // [self.collectionView headerEndRefreshing];
        [self.collectionView footerEndRefreshing];
        [collectionView reloadData];
        /**
         *加载完成,取消菊花
         */
        [self.HUD removeFromSuperview];
        
     
    }
          failure:^(NSError *error) {
              /**
               *失败时,取消菊花
               */
              [self.HUD removeFromSuperview];
              self.alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
              [self.alter show];
              [self.alter release];
              NSLog(@"%@",error);
          }];
}


/**
 *collectionView里numberOfItemsInSection(必须要实现的方法)
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.detailPictureImageArr.count;
    
}

/**
 *对于每个cell进行操作(必须要实现的方法)
 */
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailPicturesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    /**
     *创建model对象
     */
    DetailPicturesModel *coverPic = self.detailPictureImageArr[indexPath.row];
    
    /**
     *用sdwebimage对imageview进行图片赋值
     */
    [cell.detailPictureImage sd_setImageWithURL:[NSURL URLWithString:coverPic.photo]];
    /**
     *  占位图
     */
    NSURL *url = [NSURL URLWithString:coverPic.photo];
    UIImage *image = [UIImage imageNamed:@"BeanTrip.png"];
    [cell.detailPictureImage sd_setImageWithURL:url placeholderImage:image];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PictureViewController *picVC=[[PictureViewController alloc] init];
    picVC.array = self.detailPictureImageArr;
    picVC.toptitle = self.titName;
    NSUInteger integer = indexPath.row;
    picVC.integer = integer;
    [self.navigationController pushViewController:picVC animated:YES];
    [picVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
